package cn.yuanqiao.web.controller.archive.api;

import cn.yuanqiao.archive.api.domain.ReceiveRecord;
import cn.yuanqiao.archive.api.service.DetectionApiService;
import cn.yuanqiao.archive.api.service.IReceiveRecordService;
import cn.yuanqiao.archive.detection.domain.InspectionRecords;
import cn.yuanqiao.archive.wenjianzhengli.utils.FileUploadUtils;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/archive/api")
public class DetectionApiController extends BaseController {

    @Autowired
    public DetectionApiService detectionApiService;
    @Autowired
    public IReceiveRecordService receiveRecordService;


    @PostMapping("/detection")
    public AjaxResult detection(MultipartFile file,String code,String tenantCode) throws Exception {
        if(StringUtils.isEmpty(code)){
            throw new ServiceException("四性检测code不存在！请检测参数！");
        }
        Map map=detectionApiService.detection(file,code,tenantCode);
        return AjaxResult.success(map);
    }

    //导入，导出
    @Log(title = "四性检测记录", businessType = BusinessType.EXPORT)
    @PostMapping("/downLoad")
    public void zipExport(HttpServletResponse response, Long id,String tenantCode) throws IOException {

        String path=detectionApiService.zipExport(id,tenantCode);
        //将文件下载下来
        response.reset(); // 非常重要
        //在线预览，浏览器支持的文件类型都能打开
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "inline; filename=" + new Date().getTime()+".xml");
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(new File(path)));
        byte[] buf = new byte[1024];
        int len = 0;
        OutputStream out = response.getOutputStream();
        while ((len = br.read(buf)) > 0){
            out.write(buf, 0, len);
        }
        System.gc();
        br.close();
        out.close();

    }
   //接收归档包
   @PostMapping("/receive")
   public AjaxResult receive(MultipartFile file,String code,String tenantCode) throws Exception {
       if(StringUtils.isEmpty(code)){
           throw new ServiceException("四性检测code不存在！请检测参数！");
       }
       if(StringUtils.isEmpty(tenantCode)){
           throw new ServiceException("tenantCode不存在！请检测参数！");
       }

       Map <String,Object> map=detectionApiService.receive(file,code,tenantCode);
       return AjaxResult.success(map);
   }
   //获取检测结果和sxId
   @PostMapping("/getDetectionMsg")
   public AjaxResult getDetectionMsg(Long id) throws Exception {
       if(id==null){
           throw new ServiceException("包Id不存在！请检测参数！");
       }
       ReceiveRecord receiveRecord=receiveRecordService.selectReceiveRecordById(id);
       if(receiveRecord==null){
           throw new ServiceException("归档包不存在!");
       }
       if("0".equals(receiveRecord.getState())){
           throw new ServiceException("归档包未进行解析!");
       }


       Map <String,Object> map=new HashMap<>();
       map.put("result",receiveRecord.getResult());
       map.put("sxId",receiveRecord.getSxId());
       return AjaxResult.success(map);
   }

   //定时任务检测





}
